package com.alibaba.org.service.impl;

import com.alibaba.basic.dto.ShopDto;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.basic.util.MD5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.mapper.ShopAuditLogMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.org.service.IShopService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void settlement(Shop shop) {
        //校验
        //空值校验
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress()) || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getPhone()) || StringUtils.isEmpty(shop.getEmployee().getEmail())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())){
            throw new BusinessException("数据不能为空");
        }

        //密码两次校验

        //校验商铺名字是否符合规范
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!boo){
            throw new BusinessException("店铺名不符合规范！！");
        }

        //校验店铺是否曾经入驻
        Shop shopTem = shopMapper.findByNameAndAddress(shop);
        if (shopTem != null){
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }

        //入驻业务
        //保存店铺管理员信息t_employee
        Employee employee = shop.getEmployee();
        //加密
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密后的密码，存入logininfo
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //保存logininfo信息
        //将employee和logininfo表中相同字段赋值
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.add(logininfo);

        //设置employee的logininfo_id
        Long logininfoId = logininfo.getId();
        employee.setLogininfo_id(logininfoId);
        employeeMapper.add(employee);

        //2.2.保存店铺信息t_shop
        shop.setAdmin_id(employee.getId());
        shopMapper.add(shop);

        //将店铺的id更新到t_employee中
        employee.setId(shop.getId());
        employeeMapper.update(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //当前对象是店铺管理员
        logininfo.setType(0);
        return logininfo;
    }

    @Override

    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.店铺状态设置为：2
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //2.添加审核日志
        //以后做了登录要去获取登录人信息：现在先写死
        shopAuditLog.setAuditId(325L);
        shopAuditLog.setState(2);
        shopAuditLogMapper.add(shopAuditLog);
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //店铺关联的管理员
        Employee manager = employeeMapper.findOne(shop.getAdmin_id());
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setFrom("710984334@qq.com");
        //设置邮件主题
        helper.setSubject("审核失败通知");
        //邮件正文
        helper.setText("<h1>店铺激活</h1><br>"+
                "<span>点击下面的激活链接激活店铺<br>"+
                "<a href='http://localhost:8081/#/register"+shop.getId()+"'>点击激活</a>",true);
        //helper.setTo(manager.getEmail()); - 以后发给店铺入驻时的邮箱
        helper.setTo("936281626@qq.com"); //这里用我自己的邮箱做测试
        //发送消息
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.店铺状态设置为：2
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        //2.添加审核日志
        //以后做了登录要去获取登录人信息：现在先写死
        shopAuditLog.setAuditId(325L);
        shopAuditLog.setState(4);
        shopAuditLogMapper.add(shopAuditLog);
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //店铺关联的管理员
        Employee manager = employeeMapper.findOne(shop.getAdmin_id());
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setFrom("710984334@qq.com");
        //设置邮件主题
        helper.setSubject("审核失败通知");
        //邮件正文
        helper.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+shopAuditLog.getNote()+"</span><br>"+
                "<a href='http://localhost:8081/register'>点击重填资料</a>",true);
        //helper.setTo(manager.getEmail()); - 以后发给店铺入驻时的邮箱
        helper.setTo("936281626@qq.com"); //这里用我自己的邮箱做测试
        //发送消息
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getShopState() {
        return shopMapper.getShopState();
    }

    @Override
    public void shopActive(Long id) {
        Shop shop = shopMapper.findOne(id);
        if (shop.getState() != 3){
            shop.setState(3);
        }
        shopMapper.update(shop);
    }
}
